package com.cp.weixin.fun.controller;

import com.cp.weixin.entity.Result;
import com.cp.weixin.util.CharacterUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class CharController {

    @RequestMapping("/char")
    public String character() {
        return "char/char.html";
    }

    @PostMapping("/toChange")
    @ResponseBody
    public Result toChange(@RequestParam("para") String para, @RequestParam("type") String type) {
        Result result = new Result();
        if("toUnicode".equalsIgnoreCase(type)) {
            result.setData(CharacterUtil.stringToCodePoints(para));
            result.setMessage(String.valueOf(CharacterUtil.length(para)));
        } else {
            try {
                result.setData(CharacterUtil.codePointsToString(para));
            } catch (Exception ex) {
                result.setData(para);
            }
        }
        return result;
    }

}
